﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.AgentEditor.Controls
{

	/// <summary>
	/// Declaration of AgentDetailsCtrl class
	/// A control displaying the agent details, including 
	/// website and coverage countries
	/// </summary>
	public partial class AgentDetailsCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Events and Delegates

		public event EventHandler AgentTypeChanged;
		public event EventHandler AgentNameChanged;

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<string> m_countryCoverage = new List<string>();

		/////////////////////////////////////////////////////////////
		// Construction

		public AgentDetailsCtrl()
		{

			// Initialize member variables
			InitializeComponent();

			// Set controls - Type
			//cboType.Items.Add(new ComboBoxObject("Agent", (int)Definitions.eAgentType.Agent, null));
			//cboType.Items.Add(new ComboBoxObject("Solicitor", (int)Definitions.eAgentType.Solicitor, null));

		}

		/////////////////////////////////////////////////////////////
		// Event handlers


		void wnd_Closed(object sender, EventArgs e)
		{

			// Update country coverage
			_updateCountryCoverage();

		}			

		private void radUseAccountCommission_Checked(object sender, RoutedEventArgs e)
		{

			// Setup controls
			_setupControls();

		}

		private void radUseAgentCommission_Checked(object sender, RoutedEventArgs e)
		{

			// Setup controls
			_setupControls();

		}

		private void cboType_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Fire message to parent
			if (AgentTypeChanged != null)
				AgentTypeChanged(sender, e);

		}

		private void txtAgentName_TextChanged(object sender, TextChangedEventArgs e)
		{

			// Fire message to parent
			if (AgentNameChanged != null)
				AgentNameChanged(sender, e);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void Set( AgentTransport agentTransport )
		{

			// Country coverage
			m_countryCoverage.Clear();
			foreach (string strCode in agentTransport.CoverageCountryCodes)
				m_countryCoverage.Add(strCode);
			_updateCountryCoverage();

			// Setup controls
			_setupControls();

		}

		public void Get(AgentTransport agent, List<string> Errors)
		{

			// Populate the agent object with details from the control
			// and find errors in the user input

			// Check controls

            /*
			if (txtAgentName.Text.Length < 4)
				Errors.Add("The agent name must be at least 4 characters.");
			if (!(radUseAccountCommission.IsChecked ?? false))
			{
				double dblTemp = 0;
				if (!double.TryParse(txtCommissionRate.Text, out dblTemp))
					Errors.Add("The commission percentage is not a valid number.");
				else if ((dblTemp < 0) || (dblTemp > 100))
					Errors.Add("The commission percentage must be between 0 and 100.");
			}
			if(m_countryCoverage.Count==0)
				Errors.Add("The agent must have at least one country in the coverage list.");

			// Populate agent object
			agent.Name = txtAgentName.Text;
			agent.AgentType = (cboType.SelectedValue as ComboBoxObject).ItemData;
			agent.Website = txtWebsite.Text;
			agent.UseAccountCommissionRate = radUseAccountCommission.IsChecked ?? false;
			if (!agent.UseAccountCommissionRate)
				agent.SpecificCommissionRatePerc = double.Parse(txtCommissionRate.Text);
			else
				agent.SpecificCommissionRatePerc = 0;
			agent.Notes = txtNotes.Text;

			// Get the country coverage
			foreach (string strCode in m_countryCoverage)
				agent.CoverageCountryCodes.Add(strCode);
           /**/
		}

		private void _updateCountryCoverage()
		{

			// Compose comma-delimted string of countries
            /*
			StringBuilder sb = new StringBuilder();
			foreach (string strCode in m_countryCoverage)
			{
				if (sb.Length > 0)
					sb.Append(",");
				sb.Append(CachedData.Singleton.CountryMap[strCode]);
			}
			txtCoverage.Text = sb.ToString();
            /**/
		}

		private void _setupControls()
		{
            /*
			// Enable/disable controls
			txtCommissionRate.IsEnabled = (radUseAgentCommission.IsChecked ?? false);
            /**/
		}

        private void AgentEditModeToggleButton_Click(object sender, EventArgs e)
        {
            if (AgentEditModeToggleButton.IsUpsideDown)
            {
                AgentDetailsGrid.Visibility = Visibility.Collapsed;
                AgentEditModeToggleButton.IsUpsideDown = false;
            }
            else
            {
                AgentDetailsGrid.Visibility = Visibility.Visible;
                AgentEditModeToggleButton.IsUpsideDown = true;
            }
        }

	}

}
